package com.luozongyao.eduService.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luozongyao.commonutils.JwtUtils;
import com.luozongyao.commonutils.R;
import com.luozongyao.eduService.client.OrderClient;
import com.luozongyao.eduService.entity.EduCourse;
import com.luozongyao.eduService.entity.chapter.ChapterVo;
import com.luozongyao.eduService.entity.frontVo.CourseFrontVo;
import com.luozongyao.eduService.entity.frontVo.CourseWebVo;
import com.luozongyao.eduService.service.EduChapterService;
import com.luozongyao.eduService.service.EduCourseService;
import com.luozongyao.orderVo.CourseWebVoOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @program: guli_parent
 * @description:
 * @author: Zongyao Luo
 * @create: 2021-08-24 09:13
 **/
@RestController
@CrossOrigin
@RequestMapping("/eduService/courseFront")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService ;

    @Autowired
    private OrderClient orderClient;

    //1条件查询带分页查询课程
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R  getFrontCourseList(@PathVariable long page,
                                 @PathVariable long limit,
                                 @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> pageParam = new Page<>(page,limit);
        Map<String,Object> map = courseService.getFrontCourseList(pageParam,courseFrontVo);
        return R.ok().data(map);
    }

    //2课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable("courseId") String courseId, HttpServletRequest request){
        //根据课程id查询EduCourse大概信息
         CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);

        //根据课程id查询课程详情
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        //远程调用，判断课程是否被购买
        //TODO 后续补充 要判断是否登录 没登录就跳到登录页面进行登录
        boolean isBuyCourse = orderClient.isBuyCourse(JwtUtils.getMemberIdByJwtToken(request), courseId);

        return R.ok().data("chapterVideoList",chapterVideoList).data("courseWebVo",courseWebVo).data("isBuyCourse",isBuyCourse);
    }

    //根据课程ID查询课程信息
    @PostMapping("getCourseInfoOrder/{courseId}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String courseId){
        CourseWebVo baseCourseInfo = courseService.getBaseCourseInfo(courseId);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(baseCourseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }


}
